package com.itheima.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.ValidationEnum;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.common.ValidationServiceImpl;
import com.itheima.utils.Md5Util;
import com.itheima.utils.ThreadLocalUtil;
import jakarta.mail.MessagingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private ValidationServiceImpl validationService;

    @Autowired
    private UserMapper userMapper;
    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public void register(String username, String password,String email,String code) {
        validationService.validateCode(email, code, ValidationEnum.REGISTER.getCode());
        String md5password = Md5Util.getMD5String(password);
        userMapper.add(username,md5password,email);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String,Object> claims = ThreadLocalUtil.get();
        Integer id = (Integer) claims.get("id");
        userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String,Object> claims = ThreadLocalUtil.get();
        Integer id = (Integer) claims.get("id");
        userMapper.updatePwd(Md5Util.getMD5String(newPwd),id);
    }

    @Override
    public List<User> getAllUserInfo() {
        return userMapper.getAllUserInfo();
    }

    @Override
    public PageBean<User> userManage(Integer pageNum, Integer pageSize, String username, String nickname, String email) {
        PageBean<User> pb = new PageBean<User>();
        PageHelper.startPage(pageNum,pageSize);

        List<User> list = userMapper.userManage(username,nickname,email);
        Page<User> page = (Page<User>)list;
        pb.setTotal(page.getTotal());
        pb.setItems(page.getResult());
        return pb;
    }

    @Override
    public void deleteUser(Integer id) {
        Map<String,Object> claims = ThreadLocalUtil.get();
        String role = (String) claims.get("role");
        User user = userMapper.findById(id);
        if (role.equals("admin")){
            if (!user.getRole().equals("admin")){
                userMapper.deleteUser(id);
            }
        }
    }

    @Override
    public void addUser(User user) {
        userMapper.addUser(user);
    }

    @Override
    public User findByUsernameOrEmail(String username, String email) {
        return userMapper.findByUsernameOrEmail(username,email);
    }

    @Override
    public User findByEmail(String email) {
        return userMapper.findByEmail(email);
    }


    @Override
    public User loginEmail(String email, String code) {
        validationService.validateCode(email, code, ValidationEnum.LOGIN.getCode());
        // 查询用户信息
        User user = userMapper.findByEmail(email);
        return user;
    }

    @Override
    public void sendEmailCode(String email,Integer type) throws MessagingException {
        String code = RandomUtil.randomNumbers(4); // 假设你有一个工具类生成随机验证码
        validationService.saveCode(email,code,type);
    }

    @Override
    public User verifyByEmail(String email, String code) {
        validationService.validateCode(email,code, ValidationEnum.FORGET_PASS.getCode());
        User user = userMapper.findByEmail(email);
        return user;
    }

    @Override
    public void resetPwd(String newPassword,Integer id) {
        userMapper.updatePwd(Md5Util.getMD5String(newPassword),id);
    }
}
